<?php
namespace app\admin\logic;

use app\admin\model\UserModel;
use app\admin\model\GroupModel;
use app\admin\model\LoginModel;
use Session;

class UserLogic
{
	// 添加用户
	public function add_user($data)
	{
		// 状态值
		$data['status']   = empty($data['status']) ? -1 : $data['status'];
        
        // 插入
        $user = new UserModel();
        if (!empty($data['user_id'])) {
        	
        	// 为空过滤密码，否则重置密码
        	if (empty($data['password'])) {
        		unset($data['password']);
        	}else{
		        // 密码设置
				$salt             = rand(1111,9999);
				$password         = md5($data['password'].$salt."sanw");        	
				$data['salt']     = $salt;
				$data['password'] = $password;
        	}
        	
            $res  = $user->save_data($data);
        }else{
	        // 密码设置
			$salt             = rand(1111,9999);
			$password         = md5($data['password'].$salt."sanw");        	
			$data['salt']     = $salt;
			$data['password'] = $password;

            $res  = $user->insert_data($data);
        }

        return $res;
	}

	// 获取用户信息
	public function get_user_info($user_id)
	{
		// 用户信息
        $usermodel = new UserModel();
        $info = $usermodel::get($user_id);
        if (empty($info)) {
            return msg_error("数据不存在！");
        }

        // 分组信息
        $groupmodel = new GroupModel();
        $group = $groupmodel::all();

        return [
        	'info'  => $info,
        	'group' => $group,
        ];
	}

	// 编辑用户分组
	public function add_user_group($data)
	{

        // 数据补充
		$data['status']   = empty($data['status']) ? -1 : $data['status'];// 使用状态

        // 插入
        $group = new GroupModel();
        if (!empty($data['group_id'])) {
        	
            $res  = $group->save_data($data);
        }else{
            
            $res  = $group->insert_data($data);
        }

        return $res;
	}

    // 验证登录信息
    public function check_account($name, $pass)
    {
        $user = new UserModel();

        // 过滤输入信息
        $name = check_str($name);
        $pass = check_str($pass);

        if (empty($name) or empty($pass)) {
            return msg_error("账号或密码错误");
        }

        // 查询用户信息
        $info = $user->get_info(['username' => $name]);

        if (empty($info)) {
            return msg_error("账号信息不存在");
        }else{
            $verfy = md5($pass.$info['salt']."sanw");
            if ($info['password'] !== $verfy) {
                return msg_error("密码错误");
            }else{

                return $info;
            }
            
        }

    }


    // 验证登录状态
    public function check_login_status()
    {

        $userinfo = Session::get('user');
        if (empty($userinfo)) {
            session("user", null);
            return false;
        }

        if (empty($userinfo['active_time'])) {
            session("user", null);
            return false;
        }

        $diff = (int)time() - (int)strtotime($userinfo['active_time']);
        if ($diff > 1800) {
            session("user", null);
            return false;
        }
        // 更新时间
        Session::set('user.active_time', date("Y-m-d H:i:s",time()));
        return true;
    }


    // 记录日志
    public function save_login_log($user_id)
    {
        $user  = new UserModel();
        $login = new LoginModel();

        if (empty($user_id)) {
            return msg_error("用户ID不存在");
        }

        // 查询用户信息
        $info = $user->get_info(['user_id' => $user_id]);

        $data = [
            'user_id'  => $info['user_id'],
            'username' => $info['username'],
            'nickname' => $info['nickname'],
            'group_id' => $info['group_id'],
        ];
        // 获取IP
        $login_ip  = get_client_ip();
        // 登录时间
        $login_time = date("Y-m-d H:i:s", time());

        $data['login_ip']   = $login_ip;
        $data['login_time'] = $login_time;

        $res = $login->insert_data($data);
        if (isset($res['msg'])) {
            return $res;
        }

        return true;
    }
	
}